﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
public partial class Main_Master_manufacturar : System.Web.UI.Page
{
    Datautility du = new Datautility();
    DataTable dt = new DataTable();
    //For the Page Load of this Page
    #region
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["View"] != null)
            {
                if (Request.QueryString["View"].ToString() == "True")
                {                    
                    pnlview.Visible = true;
                    pnlData.Visible = false;
                    Viewdata();
                }
            }
            else
            {
                pnlview.Visible = false;
                pnlData.Visible = true;
            }
        }
    }
    #endregion
    //For Save the Manufacture Details
    #region
    protected void btnSaveManufacture_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtManufacturer.Text != "")
            {
                string strsql = "Insert Into MAS_MANUFACTURE (manufactureName)Values ('" + txtManufacturer.Text.Trim() + "')";
                int insert = du.executesql(strsql);
                if (insert > 0)
                {
                    txtManufacturer.Text = string.Empty;
                    lblMessage.Text = "Manufacturer Inserted SuccessFully.";
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Visible = true;
                }
                else
                {
                    lblMessage.Text = "Error While Manufacturer Inserting .....";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Visible = true;
                }
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Visible = true;
        }
    }
    #endregion
    //For Display the Data of Manufacture And Search.
    #region
    protected void Viewdata()
    {
        string sqlstrgrid = "select manufacID,ManufactureName,itemid from MAS_MANUFACTURE where ManufactureName != ''";
        dt = du.getdata(sqlstrgrid);
        if (dt.Rows.Count > 0)
        {
            DataView dv = dt.DefaultView;
            if (this.ViewState["SortExpression"] != null)
            {
                dv.Sort = string.Format("{0} {1}", ViewState["SortExpression"].ToString(), this.ViewState["SortOrder"].ToString());
            }
            grdmanuview.DataSource = dt;
            grdmanuview.DataBind();
        }
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {
        try
        {
            Viewdata();
            dt.DefaultView.RowFilter = "manufactureName like '%" + txtSearchManufacture.Text + "%'";
            if (dt.Rows.Count > 0)
            {
                grdmanuview.DataSource = dt;
                grdmanuview.DataBind();
            }
            else
            {
                lblViewMessage.Text = "No Record Found !";
                lblViewMessage.ForeColor = System.Drawing.Color.Green;
                lblViewMessage.Attributes.Add("style", "display:block");      
            }
        }
        catch (Exception Ex)
        {
        }
    }
    #endregion
    //For Binding the Gridview of the Manufacture and Row Updating, Editing.
    #region
    protected void grdmanuview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdmanuview.PageIndex = e.NewPageIndex;
        Viewdata();
    }
    protected void grdmanuview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdmanuview.EditIndex = -1;
        Viewdata();
    }
    protected void grdmanuview_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdmanuview.EditIndex = e.NewEditIndex;
        Viewdata();
    }
    protected void grdmanuview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            string id = grdmanuview.DataKeys[e.RowIndex].Value.ToString();
            TextBox txtmanufaName = (TextBox)grdmanuview.Rows[e.RowIndex].FindControl("txtmanufacturer");
            if (txtmanufaName.Text != "")
            {
                string sql = "update MAS_MANUFACTURE set manufactureName='" + txtmanufaName.Text.Trim() + "' where ManufacID=" + id;
                int j = du.executesql(sql);
                if (j > 0)
                {
                    lblMessage.Text = "Manufacturer Updated SuccessFully.";
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Visible = true;
                }
                else
                {
                    lblMessage.Text = "Error While Manufacturer Updating .....";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Visible = true;
                }
                grdmanuview.EditIndex = -1;
            }
            Viewdata();
        }
        catch (Exception ex)
        {

        }
    }
    protected void grdmanuview_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Sort"))
        {
            if (ViewState["SortExpression"] != null)
            {
                if (this.ViewState["SortExpression"].ToString() == e.CommandArgument.ToString())
                {
                    if (ViewState["SortOrder"].ToString() == "ASC")
                        ViewState["SortOrder"] = "DESC";
                    else
                        ViewState["SortOrder"] = "ASC";
                }
                else
                {
                    ViewState["SortOrder"] = "ASC";
                    ViewState["SortExpression"] = e.CommandArgument.ToString();
                }
            }
            else
            {
                ViewState["SortExpression"] = e.CommandArgument.ToString();
                ViewState["SortOrder"] = "ASC";
            }
            Viewdata();
        }
    }
    #endregion
    // For Export the Gridview in the Excel, Word And Excel Sheet.
    #region
    protected void imgxl_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=Manufacturar_Report.xls");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HtmlForm hf = new HtmlForm();
            grdmanuview.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            grdmanuview.Columns[grdmanuview.Columns.Count - 1].Visible = false;
            grdmanuview.AllowPaging = false;
            grdmanuview.HeaderRow.Style.Add("width", "15%");
            grdmanuview.HeaderRow.Style.Add("font-size", "10px");
            grdmanuview.Style.Add("text-decoration", "none");
            grdmanuview.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdmanuview.Style.Add("font-size", "8px");
            hf.Controls.Add(grdmanuview);
            hf.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {

        }
    }
    protected void imgword_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/vnd.word";
            Response.AddHeader("content-disposition", "attachment;filename=Manufacturar_Report.doc");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HtmlForm hf = new HtmlForm();
            grdmanuview.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            grdmanuview.Columns[grdmanuview.Columns.Count - 1].Visible = false;
            grdmanuview.AllowPaging = false;
            grdmanuview.HeaderRow.Style.Add("width", "15%");
            grdmanuview.HeaderRow.Style.Add("font-size", "10px");
            grdmanuview.Style.Add("text-decoration", "none");
            grdmanuview.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdmanuview.Style.Add("font-size", "8px");
            hf.Controls.Add(grdmanuview);
            hf.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {

        }
    }
    protected void imgpdf_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Manufacturar_Report.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            HtmlForm hf = new HtmlForm();
            grdmanuview.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            grdmanuview.Columns[grdmanuview.Columns.Count - 1].Visible = false;
            grdmanuview.AllowPaging = false;
            grdmanuview.HeaderRow.Style.Add("width", "15%");
            grdmanuview.HeaderRow.Style.Add("font-size", "10px");
            grdmanuview.Style.Add("text-decoration", "none");
            grdmanuview.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdmanuview.Style.Add("font-size", "8px");
            //grdview.DataBind();

            hf.Controls.Add(grdmanuview);
            hf.RenderControl(htw);

            //grdview.RenderControl(htw);           
            StringReader sr = new StringReader(sw.ToString());
            Document pdf = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdf);
            PdfWriter.GetInstance(pdf, Response.OutputStream);
            pdf.Open();
            htmlparser.Parse(sr);
            pdf.Close();
            Response.Write(pdf);
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        base.VerifyRenderingInServerForm(control);
    }
    #endregion
    
}